<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <script src="js/vue.js"></script>
    <script src="js/axios-0.19.0.js"></script>
    <script src="element-ui/lib/index.js"></script>
    <link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">


</head>
<body>

<style>
    .el-header {
        background-color: #B3C0D1;
        color: #333;
        text-align: center;
        line-height: 60px;
    }

    .el-main {
        background-color: #E9EEF3;
        color: #333;
        text-align: center;
        line-height: 160px;
    }

    body > .el-container {
        margin-bottom: 40px;
    }

    .el-container:nth-child(5) .el-aside,
    .el-container:nth-child(6) .el-aside {
        line-height: 260px;
    }

    .el-container:nth-child(7) .el-aside {
        line-height: 320px;
    }

    .login-box {
        border: 1px solid #DCDFE6;
        width: 350px;
        margin: 100px auto;
        padding: 35px 35px 15px 35px;
        border-radius: 5px;
        -webkit-border-radius: 5px;
        -moz-border-radius: 5px;
        box-shadow: 0 0 25px #909399;
    }

    .login-title {
        text-align: center;
        margin: 0 auto 40px auto;
        color: #303133;
    }

</style>

<div id="app">

    <el-container>
    <el-header>公告管理登陆</el-header>

    <el-main>

            <!-- ref：类似于id，给表单给个名字 model对应的实体对象 rule验证规则 -->
            <el-form ref="loginForm" :model="form" :rules="rules" label-width="60px" class="login-box">
                <h3 class="login-title">公告管理登陆</h3>
                <!-- prop指定每一项验证规则 -->
                <el-form-item label="账号" prop="adminName">
                    <!-- 双向绑定数据 -->
                    <el-input prefix-icon="el-icon-user-solid" type="text" placeholder="请输入账号" v-model="form.adminName"/>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input prefix-icon="el-icon-s-opportunity" type="password" placeholder="请输入密码" v-model="form.password"/>
                </el-form-item>
                <div style="text-align: center">
                    <!-- 点击调用onSubmit方法 -->
                    <el-button style="width:150px;" type="primary" v-on:click="onSubmit('loginForm')">登录</el-button>
                </div>
            </el-form>

    </el-main>

</el-container>

</div>


<script>
    new Vue({
        el:"#app",
        data: {
            form: {
                adminName: '',
                password: '',
            },
            token:"",
            rules: { //校验规则
                username: [
                    {required: true, message: '请输入用户名', trigger: 'blur'}
                ],
                password: [
                    {required: true, message: '请输入密码', trigger: 'blur'}
                ]
            }
        },
        methods: {
            //loginForm
            onSubmit(formName) {
                // 为表单绑定验证功能，this.$refs 获取所有ref的集合
                this.$refs[formName].validate((valid) => {
                    //验证成功
                    if (valid) {
                        //访问地址，提交的数据
                        axios.post("user/login", this.form,{
                            headers: {
                                Authorization: "gfsoaihgiodggiaoghoiasehfasfih234yt98wyrfwye389ty2389tgwe3"
                            }
                        }).then(resp=>{
                            //获取服务器返回的数据
                            let resultInfo = resp.data;
                            //注：success没有括号，idea会加上括号
                            if (resultInfo.success) {
                                this.token = resultInfo.data;
                                sessionStorage.setItem("Authorization",this.token);

                                //跳转到address.html页面
                                location.href = "/main.html";
                            }
                            else {
                                //登录失败：显示服务器返回的信息
                                this.$message.error(resultInfo.message);
                            }
                        })
                    } else {
                        //验证失败，不提交表单
                        return false;
                    }
                });
            }
        },
    });
</script>
</body>
</html>